package com.quzubuluo.quzutool;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.quzubuluo.quzutool.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Created by Administrator on 2016/6/3.
 */
public class ShowImageActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private Uri imgUri;
    private List<Uri> urls;
    private ViewPager mPager;
    private int num = 0;
    private TextView positionInTotal;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.activity_show_image);
        initView();
        imgUri = getIntent().getParcelableExtra("uri");
        urls = new ArrayList<>();
        checkUri(MainActivity.uriOne);
        checkUri(MainActivity.uriTwo);
        checkUri(MainActivity.uriThree);
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(this);
        mPager.setAdapter(myPagerAdapter);
        mPager.addOnPageChangeListener(this);
        num = urls.indexOf(imgUri);
        mPager.setCurrentItem(num);
        num++;
        positionInTotal.setText(num + " / " + urls.size());
    }

    private void checkUri(Uri uri) {
        if (uri != null) {
            urls.add(uri);
        }
    }

    private void initView() {
        mPager = (ViewPager) findViewById(R.id.show_vp);
        positionInTotal = (TextView) findViewById(R.id.show_title);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        position++;
        positionInTotal.setText(position + " / " + urls.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    public class MyPagerAdapter extends PagerAdapter {
        private Context context;

        public MyPagerAdapter(Context context) {
            this.context = context;
        }

        @Override
        public View instantiateItem(final ViewGroup container, int position) {

//            Log.i("chen", "instantiateItem: "+urls.get(position));
            Uri path = urls.get(position);
            final PhotoView photoView = new PhotoView(context);
            Glide.with(context).load(path).asBitmap().into(photoView);
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShowImageActivity.this.finish();
                }
            });
            container.addView(photoView);
            return photoView;

        }

        @Override
        public int getCount() {
            return urls.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
